//
//  ViewController.m
//  AddressList
//
//  Created by joker on 15/7/23.
//  Copyright (c) 2015年 joker. All rights reserved.
//

#import "LoginViewController.h"
#import "ServerRequestUtility.h"
#import "Constants.h"
#import "AddressListTableViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

#pragma mark - custome functions

- (IBAction)LogInBtn:(UIButton *)sender {
    
    NSString * userName = self.UserName.text;
    NSString * md5_password =  [ServerRequestUtility md5:self.PassWord.text];
    
    NSDictionary * params = @{
                              kLogInName: userName,
                              kPassWord: md5_password
                              };
    
    NSURL * LogInURL = [NSURL URLWithString:LogInAPI relativeToURL: [NSURL URLWithString: serverSite]];
    WebService * server = [[WebService alloc] initWithURL:LogInURL AndRequestParam:params Delegate:self];
    server.type = GetUserInfo;
}

#pragma mark - WebServiceDelegate
-(void)processReturnValue:(WebService *)server ReturnValue:(NSString *)returnValue{
    if(server.type == GetUserInfo){
        self.userInfoDictionary = [NSJSONSerialization JSONObjectWithData:[returnValue dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
#ifdef  DUMP_INFO
        NSLog(@"LogIn Request return userInfo = \n%@", self.userInfoDictionary);
#endif
        [self performSegueWithIdentifier:@"showAddressTableViewList" sender:self];
    }
}

#pragma mark - Segue Method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString: @"showAddressTableViewList"]){
        AddressListTableViewController *tableViewController = (AddressListTableViewController *)[[segue destinationViewController] topViewController];
        
        tableViewController.userInfo = self.userInfoDictionary;
#ifdef DUMP_INFO
        NSLog(@"Prepare for showAddressTableViewList Segue  called!");
#endif
    }
    
}
#pragma  mark - ViewController relevation functions

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.LogIn setEnabled:NO];
    
    self.UserName.delegate = self;
    self.PassWord.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//Hide Status Bar
-(BOOL)prefersStatusBarHidden{
    return YES;
}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField        // return NO to disallow editing.
{
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField           // became first responder
{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
{
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
{
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string   // return NO to not change text
{
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField               // called when clear button pressed. return NO to ignore (no notifications)
{
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField              // called when 'return' key pressed. return NO to ignore.
{
    [self.UserName resignFirstResponder];
    [self.PassWord resignFirstResponder];
    return YES;
}

#pragma mark - UITextFieldDelegateNotification
-(void)TextFieldTextDidChangeNotification: (NSNotification *) aNotification {
    if([self.UserName.text isEqualToString:@""] || [self.PassWord.text isEqualToString:@""] )
    {
        [self.LogIn setEnabled:NO];
    }
    else
    {
        [self.LogIn setEnabled:YES];
    }
}
#pragma  mark - Gesture Function
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.UserName resignFirstResponder];
    [self.PassWord resignFirstResponder];
}
@end
